package cn.flying.system.service.cm.impl;

import javax.annotation.Resource;

import java.util.HashMap;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import cn.flying.base.common.vo.PageRequest;
import cn.flying.base.common.vo.PageResponse;
import cn.flying.system.domain.cm.CmDictCode;
import cn.flying.system.domain.cm.CmDictCodeExample;
import cn.flying.system.domain.cm.CmDictType;
import cn.flying.system.domain.cm.CmDictTypeExample;
import cn.flying.system.mapper.cm.CmDictCodeMapper;
import cn.flying.system.mapper.cm.CmDictTypeMapper;
import cn.flying.system.service.cm.CmDictTypeService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

/**
 * @description: 枚举字典类型服务接口实现类
 * @author: lvyong
 * @date: 2022年05月22日 23:08
 * @version: 1.0
 */
@Service
public class CmDictTypeServiceImpl implements CmDictTypeService {
    @Resource
    private CmDictTypeMapper cmDictTypeMapper;
    @Resource
    private CmDictCodeMapper cmDictCodeMapper;

    /**
     * 查询分页列表数据
     *
     * @param pageRequest
     * @return
     */
    @Override
    public PageResponse<CmDictType> getDictTypePageList(PageRequest<CmDictType> pageRequest) {
        Integer pageNum = pageRequest.getPageNum();
        Integer pageSize = pageRequest.getPageSize();
        CmDictType cmDictType = pageRequest.getParam();
        CmDictTypeExample example = new CmDictTypeExample();
        CmDictTypeExample.Criteria criteria = example.createCriteria();
        if (StringUtils.hasText(cmDictType.getDictType())) {
            criteria.andDictTypeEqualTo(cmDictType.getDictType());
        }
        if (StringUtils.hasText(cmDictType.getStatus())) {
            criteria.andStatusEqualTo(cmDictType.getStatus());
        }
        example.setOrderByClause(" created_time desc ");
        PageHelper.offsetPage((pageNum - 1) * pageSize, pageSize);
        Page<CmDictType> page = (Page<CmDictType>) cmDictTypeMapper.selectByExample(example);
        return new PageResponse(page.getTotal(), pageNum, page.getResult());
    }

    /**
     * 根据主键id查询
     *
     * @param id
     * @return
     */
    @Override
    public CmDictType getDictTypeById(Long id) {
        return cmDictTypeMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询所有的类型
     *
     * @return
     */
    @Override
    public List<CmDictType> getDictTypeList() {
        CmDictTypeExample example = new CmDictTypeExample();
        CmDictTypeExample.Criteria criteria = example.createCriteria();
        return cmDictTypeMapper.selectByExample(example);
    }

    /**
     * 保存
     *
     * @param cmDictType
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void insertDictType(CmDictType cmDictType) {
        if (StringUtils.hasText(cmDictType.getStatus()) && "on".equals(cmDictType.getStatus())) {
            cmDictType.setStatus("Y");
        } else {
            cmDictType.setStatus("N");
        }
        cmDictTypeMapper.insertSelective(cmDictType);
    }

    /**
     * 根据主键ID更新
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateDictTypeById(CmDictType cmDictType) {
        if (StringUtils.hasText(cmDictType.getStatus()) && "on".equals(cmDictType.getStatus())) {
            cmDictType.setStatus("Y");
        } else {
            cmDictType.setStatus("N");
        }
        cmDictTypeMapper.updateByPrimaryKeySelective(cmDictType);
    }

    /**
     * 删除字典
     *
     * @param id
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delDictType(Long id) {
        CmDictType dictType = cmDictTypeMapper.selectByPrimaryKey(id);
        CmDictCodeExample example = new CmDictCodeExample();
        CmDictCodeExample.Criteria criteria = example.createCriteria();
        criteria.andDictTypeEqualTo(dictType.getDictType());
        cmDictCodeMapper.deleteByExample(example);

        cmDictTypeMapper.deleteByPrimaryKey(id);
    }
}
